package service.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.dao.UserRepository;
import service.entity.UserEntity;

/**
 * Created by Administrator on 2016/10/31.
 */
@Controller
public class SampleController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;
    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public int add(@RequestParam("a") int a, @RequestParam("b") int b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        int r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
//        UserEntity  user = myJdbcQuery.getUser(1);
//        logger.info("---------------------------- user:" + user +"----------------------------" );
        return r;
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    @ResponseBody
    public UserEntity getUserById(@PathVariable("id") String id) {
        logger.info("getUserById ---" + id);
        int i_id = Integer.parseInt(id);
        UserEntity user = userRepository.findById(i_id);
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
        return user;
    }
}
